<template>
  <div>
    <!-- Form -->
    <el-button @click="handleCreate">增加</el-button>
    <el-form ref="form">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="账号类型" width="180">
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ scope.row.power }}</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名" width="180">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>账号：{{ scope.row.adminName }}</p>
              <p>密码: {{ scope.row.adminPassword }}</p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.name }}</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="电话号码" width="180">
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ scope.row.phone }}</span>
          </template>
        </el-table-column>
        <el-table-column label="邮箱" width="180">
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ scope.row.email }}</span>
          </template>
        </el-table-column>
        <el-table-column label="账号状态" width="180">
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ scope.row.status }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="账号" :label-width="formLabelWidth">
          <el-input v-model="form.adminName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="form.adminPassword" autocomplete="off" />
        </el-form-item>
        <el-form-item label="名字" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input v-model="form.phone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="form.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="账号状态" :label-width="formLabelWidth">
          <el-input v-model="form.status" autocomplete="off">0</el-input>
        </el-form-item>
        <el-form-item label="账号类型" :label-width="formLabelWidth">
          <el-select v-model="form.power" placeholder="请选择账号类型">
            <el-option label="供应商" value="1" />
            <el-option label="门店" value="2" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?addAdmin():adminUpdate()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addAdmin, getAllAdmin, deleteAdmin, adminUpdate } from "@/api/admin";
import { log } from "util";
export default {
  data() {
    return {
      dialogTableVisible: false,
      dialogFormVisible: false,
      listQuery: {
        page: 1,
        limit: 10,
        area: undefined,
        title: undefined,
        importance: undefined,
        type: {},
        sort: -1
      },
      dialogStatus: "",
      textMap: {
        update: "修改",
        create: "新增"
      },
      form: {
        name: "",
        phone: "",
        email: "",
        adminName: "",
        adminPassword: "",
        status: 0,
        power: 1
      },
      tableData: [],
      formLabelWidth: "120px"
    };
  },
  created() {
    this.getAllAdmin();
  },
  methods: {
    getAllAdmin() {
      getAllAdmin(this.listQuery).then(res => {
        this.tableData = res.data.items;
        this.total = res.data.title;
      });
    },
   handleCreate() {
      this.rest()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    rest() {
      this.form = {
        name: "",
        phone: "",
        email: "",
        adminName: "",
        adminPassword: "",
        status: 0,
        power: 1
      };
    },
    async addAdmin() {
      let res = await addAdmin(this.form);
      if (res._id) {
        this.getAllAdmin();
        this.rest();
        this.dialogFormVisible = false;
      }
    },
    handleEdit(index, row) {
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.form = row;
        this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    adminUpdate() {
      console.log(this.form);
      
      adminUpdate(this.form).then(res => {
        this.getAllAdmin()
        this.dialogFormVisible = false;
        this.rest();
      });
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteAdmin(row._id).then(res => {
            this.getAllAdmin();
          });
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style>
</style>
